﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace L03
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入计算项数：");
            int n;
            while (!int.TryParse(Console.ReadLine(), out n))
            {
                Console.WriteLine("请输入计算项数:");
            }
            double pi = 0;
            pi = 4 * Fact(n);
            Console.WriteLine("圆周率的近似值为:{0}" , pi);
            Console.ReadKey();
        }

        public static double  Fact(int x)
        {
            double a;
            double  result = 0;
            do
            {
                a = Math.Pow(-1, x + 1);
                result += 1.0 / (2 * x - 1) * a;
                x--;
            }
            while (x > 0);
            return result;
        }

    }
}
